#include "xors3d.h"
#include <cmath>
#include <iostream>

using namespace std;

/*class IMoveBehaviour
{
	public:		
		virtual void move(const Handle object) = 0;
		virtual ~IMoveBehaviour() {};
};

class MoveWithKey : public IMoveBehaviour
{
	public:
		MoveWithKey() { speed = 0.5f; }
		virtual ~MoveWithKey() {};
		virtual void move(const Handle object);
	private:
		float speed;
};

class MoveWithMouse : public IMoveBehaviour
{
	public:
		MoveWithMouse() { speed = 0.1f; goal = xCreatePivot(); }
		virtual ~MoveWithMouse() {};
		virtual void move(const Handle object);
	private:
		float speed;
		Handle goal;
};

void MoveWithKey::move(const Handle object)
{
	if(xKeyDown(203)) xMoveEntity(object, -speed, 0, 0);
	if(xKeyDown(205)) xMoveEntity(object, speed, 0, 0);
	if(xKeyDown(200)) xMoveEntity(object, 0, speed, 0);
	if(xKeyDown(208)) xMoveEntity(object, 0, -speed, 0);
}

void MoveWithMouse::move(const Handle object)
{

}



class Level
{
	private:
		User user;
		Handle terrain;
		Handle camera;

		void Collisions();
	public:
		Level();
		void Load();
		void Update();	
};*/

int main()
{	
	xGraphics3D(1024, 768, 32);
	xSetBuffer(xBackBuffer());
	
	while(!xKeyHit(KEY_ESCAPE))
	{
		xRenderWorld();
		xFlip();
	}
}

/*User::User(float x, float y, float z)
{
	CreateUser(x, y, z);
}

void User::CreateUser(float x, float y, float z)
{
	sphere = xCreateSphere();
	xPositionEntity(sphere, x, y, z);
	speed = 0.1f;
}

void User::Update()
{
	Move();
}


void User::Move()
{
	if(xKeyDown(203)) xMoveEntity(sphere, -speed, 0, 0);
	if(xKeyDown(205)) xMoveEntity(sphere, speed, 0, 0);
	if(xKeyDown(200)) xMoveEntity(sphere, 0, speed, 0);
	if(xKeyDown(208)) xMoveEntity(sphere, 0, -speed, 0);

	if(xMouseDown(MOUSE_RIGHT))
	{	
		cout << xPickedX() << " " << xPickedY() << " " << xPickedZ() << endl;		
	}

	/*float distance = xEntityDistance(sphere, goal);
	if(distance > 1)
	{
		float nx = (xEntityX(sphere) - xEntityX(goal)) / distance;
		float ny = (xEntityY(sphere) - xEntityY(goal)) / distance;
		xMoveEntity(sphere, -speed * nx, -speed * ny, 0);
	}
}

int User::Sign(int i)
{
	if(i == 0)
		return 0;
	else if(i > 0)
		return 1;
	else if(i < 0)
		return -1;
}

Level::Level()
{

	

	Handle light = xCreateLight();
}

void Level::Load()
{
	terrain = xCreateCube();
	xPositionEntity(terrain, 0, 0, 0);
	xScaleEntity(terrain, 40, 40, 0.1);
	xEntityPickMode(terrain, 2);

	Handle texture = xLoadTexture("../Media/terrain.jpg");
	xEntityTexture(terrain, texture);
	xFreeTexture(texture);	

	//Collisions();
}

void Level::Update()
{
	user.Update();
}

void Level::Collisions()
{
	const int TERRT = 0;
	const int USERT = 1;
	xEntityType(terrain, TERRT);
	xEntityType(user.GetUserHandle(), USERT);

	xCollisions(USERT, TERRT, SPHERETOTRIMESH, RESPONSE_SLIDING_DOWNLOCK);
}*/